package nl.vu.few.ibivu.biomodels.client.ui;

import nl.vu.few.ibivu.biomodels.client.ui.view.PreferencesView;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class PreferencesViewImpl extends SectionStack implements PreferencesView {

	Presenter presenter;
	DynamicForm userDetailsForm;
	TextItem firstName;
	TextItem lastName;
	ButtonItem saveUserDetails;
	
	DynamicForm preferencesForm;
	CheckboxItem smartConnections;
	ButtonItem savePreferences;
	
	@Inject
	public PreferencesViewImpl() {
		super();
		this.setWidth100();
		this.setHeight100();
		
		userDetailsForm = new DynamicForm();
		firstName = new TextItem();
		firstName.setTitle("First Name");
		firstName.setRequired(true);
		lastName = new TextItem();
		lastName.setTitle("Last Name");
		lastName.setRequired(true);
		saveUserDetails = new ButtonItem();
		saveUserDetails.setTitle("Save");
		saveUserDetails.addClickHandler(new ClickHandler(){
			
			@Override
			public void onClick(ClickEvent event) {
				if (userDetailsForm.validate()) {
					presenter.setFirstName(firstName.getValueAsString());
					presenter.setLastName(lastName.getValueAsString());
					presenter.save();
				}
				
			}});
		userDetailsForm.setFields(firstName, lastName, saveUserDetails);
		
		SectionStackSection userDetailsSection = new SectionStackSection("Your Details");
		userDetailsSection.setCanReorder(false);
		userDetailsSection.setResizeable(false);
		userDetailsSection.setExpanded(true);
		userDetailsSection.addItem(userDetailsForm);
		this.addSection(userDetailsSection);
		
		
		preferencesForm = new DynamicForm();
		smartConnections = new CheckboxItem();
		smartConnections.setTitle("Use Smart Connections");
		savePreferences = new ButtonItem();
		savePreferences.setTitle("Save");
		savePreferences.addClickHandler(new ClickHandler(){
			
			@Override
			public void onClick(ClickEvent event) {
				if (preferencesForm.validate()) {
					//TODO Implement smartConnections
					presenter.save();
				}
				
			}});
		preferencesForm.setFields(smartConnections, savePreferences);
		SectionStackSection preferencesSection = new SectionStackSection("Application Preferences");
		preferencesSection.setCanReorder(false);
		preferencesSection.setResizeable(false);
		preferencesSection.addItem(preferencesForm);
		this.addSection(preferencesSection);
		
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		firstName.setValue(presenter.getFirstName());
		lastName.setValue(presenter.getLastName());
		// TODO Implement!
		smartConnections.setValue(false);
	}

}
